package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 保存套餐和关系表数据
     * @param setmealDto
     */
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐
        save(setmealDto);
        //保存套餐和菜品关系表
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            //设置分类id
            setmealDish.setSetmealId(setmealDto.getId());
        });

        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐 和菜品关系表
     *
     * @param ids
     */
    @Override
    @Transactional
    public void removeByDish(List<Long> ids) {
        //删除套餐
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getStatus, 1);
        lqw.in(Setmeal::getId, ids);
        int count = this.count(lqw);
        if (count > 0) {
            throw new CustomException("套餐正在售卖中,不能删除");
        }
        this.removeByIds(ids);

        //删除关系表
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(lambdaQueryWrapper);

    }

    /**
     * 根据id查询套餐信息和对应的菜品信息(回显)
     * @param id
     * @return
     */
    @Override
    public SetmealDto getByIdWithFlavor(Long id) {
        //查询套餐基本信息
        Setmeal setmeal = getById(id);
        //创建一个SetmealDto对象
        SetmealDto setmealDto = new SetmealDto();
        //拷贝
        BeanUtils.copyProperties(setmeal,setmealDto);
        //构造查询构造器
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        //添加查询条件
        lqw.eq(SetmealDish::getSetmealId,setmeal.getId());
        //执行查询
        List<SetmealDish> dishList = setmealDishService.list(lqw);
        //给对象菜品信息赋值
        setmealDto.setSetmealDishes(dishList);
        return setmealDto;

    }

    /**
     * 修改套餐信息，同时修改对应的菜品信息
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(SetmealDto setmealDto) {
        //修改setmeal基本信息
        this.updateById(setmealDto);

        //删除之前套餐关联的菜品信息
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(lqw);

        //获取修改后的菜品数据，并给菜品信息设置setmeal_id
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
        });

        //保存菜品信息
        setmealDishService.saveBatch(setmealDishes);
    }
}
